﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Timers;
using System.IO;
using System.Net;
using System.Text;

/// <summary>
///SendBlog 的摘要说明
/// </summary>
public class SendBlog
{
    private Timer timer;
    private const string COMMENT_URL = "https://api.weibo.com/2/comments/create.json";
    private const string NEW_WEIBO = "https://api.weibo.com/2/statuses/update.json";
    private const string COMMENT_REPLY = "https://api.weibo.com/2/comments/reply.json";
    public SendBlog()
    {
        //
        //TODO: 在此处添加构造函数逻辑
        //
    }

    public void Start()
    {
        if (timer == null)
        {
            timer = new Timer(1000 * 120);
            timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
        }
        timer.Start();

    }

    public void Stop()
    {
        if (timer != null)
            timer.Stop();
    }

    protected void timer_Elapsed(object sender, ElapsedEventArgs e)
    {
        //取出超过当前时间的微博
        List<DelayWeiboData> list = new DBDelaySendWeibo().SelectList(DateTime.UtcNow);
        if (list != null)
        {
            for (int i = 0; i < list.Count; i++)
            {
                if (list[i].weiboId == "0")
                    SendWeibo(list[i]);
                else if (string.IsNullOrEmpty(list[i].commentId))
                    SendComment(list[i]);
                else
                    SendReplyComment(list[i]);
            }
        }
    }

    private void SendWeibo(DelayWeiboData data)
    {
        BlogAccountData blogAccount = new DBBlogAccount().Select(data.replyAccount);
        if (blogAccount != null)
        {
            string args = string.Format("access_token={0}&status={1}", blogAccount.accessToken, HttpUtility.UrlEncode(data.reply));
            try
            {
                URLRequest.Post(NEW_WEIBO, args);
                new DelayWeiboProxy().Delete(data.id);
            }
            catch (Exception error)
            {
                Log.Write(args + "\r\n" + error.ToString());
            }
        }
    }

    private void SendComment(DelayWeiboData data)
    {
        BlogAccountData blogAccount = new DBBlogAccount().Select(data.replyAccount);
        if (blogAccount != null)
        {
            string args = string.Format("access_token={0}&id={1}&comment={2}", blogAccount.accessToken, data.weiboId, HttpUtility.UrlEncode(data.reply));
            try
            {
                URLRequest.Post(COMMENT_URL, args);
                new DelayWeiboProxy().Delete(data.id);
            }
            catch (Exception error)
            {
                Log.Write(args + "\r\n" + error.ToString());
            }
        }
    }

    private void SendReplyComment(DelayWeiboData data)
    {
        BlogAccountData blogAccount = new DBBlogAccount().Select(data.replyAccount);
        if (blogAccount != null)
        {
            string args = string.Format("access_token={0}&cid={1}&id={2}&comment={3}", blogAccount.accessToken, data.commentId, data.weiboId, HttpUtility.UrlEncode(data.reply));
            try
            {
                URLRequest.Post(COMMENT_REPLY, args);
                new DelayWeiboProxy().Delete(data.id);
            }
            catch (Exception error)
            {
                Log.Write(args + "\r\n" + error.ToString());
            }
        }
    }
}